package com.alex.statistics.controller;

import com.alex.statistics.method.structureanalysis.StaticStructureService;
import com.alex.statistics.method.structureanalysis.DynamicStructureService;
import com.alex.statistics.pojo.Result;
import com.alex.statistics.pojo.request.structureanalysis.StaticStructureRequest;
import com.alex.statistics.pojo.request.structureanalysis.DynamicStructureRequest;
import com.alex.statistics.pojo.result.structureanalysis.StaticStructureResult;
import com.alex.statistics.pojo.result.structureanalysis.DynamicStructureResult;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@Tag(name = "结构分析", description = "包含静态结构分析和动态结构分析接口")
@RequestMapping("/structure")
public class StructureAnalysisController {

    @Autowired
    private StaticStructureService staticStructureService;

    @Autowired
    private DynamicStructureService dynamicStructureService;

    // ======================== 静态结构分析 ========================

    @PostMapping("/static/proportion")
    @Operation(summary = "占比分析", description = """
            ### 功能说明
            分析各组成部分相对于整体的重要性或规模，计算各部分占整体的百分比
            
            ### 示例输入
            ```json
            {
              "componentNames": ["产品A", "产品B", "产品C"],
              "componentValues": [150, 250, 100],
              "totalValue": 500.0
            }
            ```
            """)
    public Result<StaticStructureResult.ProportionResult> calculateProportion(
            @RequestBody @Parameter(description = "占比分析请求参数") StaticStructureRequest.ProportionRequest request) {
        return Result.ok(staticStructureService.calculateProportion(request));
    }

    @PostMapping("/static/composition-ratio")
    @Operation(summary = "构成比分析", description = """
            ### 功能说明
            分析多分类变量的分布情况，计算各分类占总体的比例
            
            ### 示例输入
            ```json
            {
              "categoryNames": ["华东", "华北", "华南", "西部"],
              "categoryValues": [300, 200, 150, 350]
            }
            ```
            """)
    public Result<StaticStructureResult.CompositionRatioResult> calculateCompositionRatio(
            @RequestBody @Parameter(description = "构成比分析请求参数") StaticStructureRequest.CompositionRatioRequest request) {
        return   Result.ok(staticStructureService.calculateCompositionRatio(request));
    }

    @PostMapping("/static/pareto")
    @Operation(summary = "帕累托分析", description = """
            ### 功能说明
            基于二八定律识别对结果影响最大的关键因素，默认累计占比达80%的因素为关键因素
            
            ### 示例输入
            ```json
            {
              "factorNames": ["缺陷A", "缺陷B", "缺陷C", "缺陷D", "缺陷E"],
              "impactValues": [120, 80, 50, 30, 20],
              "threshold": 80.0
            }
            ```
            """)
    public Result<StaticStructureResult.ParetoResult> calculatePareto(
            @RequestBody @Parameter(description = "帕累托分析请求参数") StaticStructureRequest.ParetoRequest request) {
          return Result.ok(staticStructureService.calculatePareto(request));
    }

    // ======================== 动态结构分析 ========================

    @PostMapping("/dynamic/structure-change")
    @Operation(summary = "结构变化指数分析", description = """
            ### 功能说明
            分析两个时期（基期和报告期）各组成部分的结构变动幅度，计算结构变化指数
            
            ### 示例输入
            ```json
            {
              "componentNames": ["原材料", "人工", "设备", "杂费"],
              "basePeriodValues": [200, 300, 150, 50],
              "reportPeriodValues": [250, 350, 180, 70]
            }
            ```
            """)
    public Result<DynamicStructureResult.StructureChangeIndexResult> calculateStructureChangeIndex(
            @RequestBody @Parameter(description = "结构变化指数请求参数") DynamicStructureRequest.StructureChangeIndexRequest request) {
          return Result.ok(dynamicStructureService.calculateStructureChangeIndex(request));
    }

    @PostMapping("/dynamic/contribution")
    @Operation(summary = "贡献度分析", description = """
            ### 功能说明
            分析各因素对整体指标变化的贡献值和贡献比例，评估因素的重要性
            
            ### 示例输入
            ```json
            {
              "factorNames": ["销量", "单价", "成本"],
              "basePeriodFactors": [100, 50, 30],
              "reportPeriodFactors": [120, 55, 28],
              "basePeriodTotal": 2000.0,
              "reportPeriodTotal": 3200.0
            }
            ```
            """)
    public Result<DynamicStructureResult.ContributionResult> calculateContribution(
            @RequestBody @Parameter(description = "贡献度分析请求参数") DynamicStructureRequest.ContributionRequest request) {
          return Result.ok(dynamicStructureService.calculateContribution(request));
    }
}
    